package by.bsu.touragency.tag;

import java.io.IOException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.log4j.Logger;
import by.bsu.touragency.command.guest.ConfigurationManager;
import by.bsu.touragency.db.beans.Person;
import by.bsu.touragency.db.dao.PersonDAO;

public class AdminPanelTag extends BodyTagSupport {
	
	private static final long serialVersionUID = 4036551590994850809L;

	private static Logger logger=Logger.getLogger("log");

	private PersonDAO cd = new PersonDAO();
	private Person c = new Person();
	private String login;
	
	public int doStartTag() throws JspTagException {
		logger.info("Admin panel tag After body");
		String login = (String) pageContext.getSession().getAttribute("user");
		if (login != "" && login != null) {
			c = cd.readByLogin(login);
			if (c.isAdmin()) {
				try {
					pageContext.getOut().write("<li><h2>AdminPanel</h2><ul>");
					
					pageContext.getOut().write("<li><a href=\"");
					pageContext.getOut().write(pageContext.getServletContext().getContextPath()+
							ConfigurationManager.getInstance().getProperty(
							ConfigurationManager.ADD_TOUR_PAGE_PATH));
					pageContext.getOut().write("\">Add tour</a></li>");
					
					pageContext.getOut().write("<li><a href=\"");
					pageContext.getOut().write(pageContext.getServletContext().getContextPath()+
							ConfigurationManager.getInstance().getProperty(
							ConfigurationManager.MAIN_PAGE_PATH));
					pageContext.getOut().write("\">View all NB tours</a></li>");
					
					pageContext.getOut().write("<li><a href=\"");
					pageContext.getOut().write(pageContext.getServletContext().getContextPath()+
							ConfigurationManager.getInstance().getProperty(
							ConfigurationManager.REGISTRATION_ADMIN_PAGE_PATH));
					pageContext.getOut().write("\">Add client and admin</a></li></ul></li>");
					
				} catch (IOException e) {
					logger.error("Error in Admin panel tag");
				}

			}
		}
		return EVAL_BODY_INCLUDE;
	}

	public int doAfterBody() throws JspTagException {
		
		return SKIP_BODY;
	}
	
	public int doEndTag() throws JspTagException {
		return SKIP_BODY;
	}
	public void setUser(String user) {
		this.setLogin(new String(user));
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getLogin() {
		return login;
	}


	
}
